
#ifndef __TAGS_LIB_H__
#define __TAGS_LIB_H__

#ifdef __cplusplus 
extern "C" {
#endif

/*
 * Tags with links within them
 */
enum TagType {
	TAG_ANCHOR,
	TAG_APPLET,
	TAG_AREA,
	TAG_BGSOUND,
	TAG_BODY,
	TAG_EMBED,
	TAG_FORM,
	TAG_FRAME,
	TAG_IFRAME,
	TAG_IMG,
	TAG_INPUT,
	TAG_ISINDEX,
	TAG_LINK,
	TAG_SCRIPT,

	TAG_OTHER,
	TAG_NONE,
	TAG_COMMENT
};
typedef enum TagType TagType;

#define TAG_FLAG_END   0x8000
#define IS_TAG_END(t)  ((t) & TAG_FLAG_END)
#define GET_TAG(t)     ((t) & ~TAG_FLAG_END)
#define SET_TAG_END(t) ((t) | TAG_FLAG_END)

char *tag_name[] = {
	"a",
	"applet",
	"area",
	"bgsound",
	"body",
	"embed",
	"form",
	"frame",
	"iframe",
	"img",
	"input",
	"isindex",
	"link",
	"script",

	0
};

#define MAX_ATTR 3
char *tag_linkattr[][MAX_ATTR] = {
	{ "href", 0, 0 },      // TAG_ANCHOR

	{ "code", 0, 0 },      // TAG_APPLET
	{ "href", 0, 0 },      // TAG_AREA
	{ "src",  0, 0 },      // TAG_BGSOUND
	{ "stylesrc", 0, 0 },  // TAG_BODY
	{ "src",  0, 0 },      // TAG_EMBED
	{ "action", 0, 0 },    // TAG_FORM
	{ "src", 0, 0 },       // TAG_FRAME
	{ "src", 0, 0 },       // TAG_IFRAME
	{ "src", "lowsrc", "dynsrc" }, // TAG_IMG
	{ "src", "lowsrc" },   // TAG_INPUT
	{ "action", 0, 0 },    // TAG_ISINDEX
	{ "src", "href", 0 },  // TAG_LINK
	{ "src", 0, 0 },       // TAG_SCRIPT

	{ 0, 0, 0 }            // TAG_OTHER
};


struct LinkElem {
	char *start;
	char *end;
};
typedef struct LinkElem LinkElem;

struct Tag {
	char *start;
	char *end;
	TagType type;

	LinkElem links[MAX_ATTR];

#define link_start links[0].start
#define link_end links[0].end
};
typedef struct Tag Tag;


/*
 * API
 */
Tag next_tag(char *s, int inscript);
char *parse_tag(Tag *t);

#ifdef __cplusplus 
};
#endif

#endif /* __TAGS_LIB_H__ */
